package com.sduept.nwld.dataserver.manager.statistics;

import com.sduept.bigdata.fault.entity.BreakerActionInfo;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.faulttimeanalysis.BreakerActTimeCount;
import com.sduept.nwld.dataserver.model.faulttimeanalysis.FaultContinuedTimeCount;
import com.sduept.nwld.dataserver.model.faulttimeanalysis.ProtectActTimeCount;
import com.sduept.utils.DateUtil;
import com.sduept.utils.SpringContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FaultTimeAnalysisManager {

	@Autowired
	private SuperDAO dao;
	@Autowired
	private StatisticsCommonManager comM;

	/**
	 * ***************************故障持续时间**************************
	 * 
	 * @param bean
	 */
	public void createOrUpdate(FaultContinuedTimeCount bean) {
		dao.createOrUpdate(bean, FaultContinuedTimeCount.class);
	}

	/**
	 * 查询月度数据
	 * 
	 * @param year
	 * @param month      为null，则查询1-12月份的数据
	 * @param voltage
	 * @param subCompany
	 * @return
	 */
	public List<FaultContinuedTimeCount> findMonthFaultContinuedTimeCount(Integer year, Integer month, Integer voltage, String subCompany) {
		List<FaultContinuedTimeCount> list = new ArrayList<FaultContinuedTimeCount>();
		if (null != year) {
			List<Object> values = new ArrayList<>();
			String sql = "year = ? ";
			values.add(year);
			if (month != null) {
				sql += " and month = ? ";
				values.add(month);
			} else {
				sql += " and month is not null ";
			}
			if (StringUtils.isNotEmpty(subCompany)) {
				sql += " and subCompany = ? ";
				values.add(subCompany);
			}
			if (voltage != null) {
				sql += " and voltage = ? ";
				values.add(voltage);
			}
			sql += " order by month";
			list = dao.findByCondition(FaultContinuedTimeCount.class, sql, values.toArray());
		}
		return list;
	}

	/**
	 * 查询年度数据（month为null）
	 * 
	 * @param year
	 * @param voltage
	 * @param subCompany
	 * @return
	 */
	public List<FaultContinuedTimeCount> findYearFaultContinuedTimeCount(Integer year, Integer voltage, String subCompany) {
		List<FaultContinuedTimeCount> list = new ArrayList<FaultContinuedTimeCount>();
		if (null != year) {
			List<Object> values = new ArrayList<>();
			String sql = "year = ? and month is null";
			values.add(year);
			if (StringUtils.isNotEmpty(subCompany)) {
				sql += " and subCompany = ? ";
				values.add(subCompany);
			}
			if (voltage != null) {
				sql += " and voltage = ? ";
				values.add(voltage);
			}
			sql += " order by year";
			list = dao.findByCondition(FaultContinuedTimeCount.class, sql, values.toArray());
		}
		return list;
	}

	public List<FaultContinuedTimeCount> findAllFaultContinuedTimeCount() {
		return dao.findAll(FaultContinuedTimeCount.class);
	}

	public void deleteFaultContinuedTimeCount(String id) {
		dao.delete(id, FaultContinuedTimeCount.class);
	}

	public void deleteFaultCount(Integer year, Integer month) {
		if (null != month) {
			if (0 == month) {
				// 删除当年1-12月份的数据
				dao.delete(FaultContinuedTimeCount.class, "year=? and month is not null", year);
			} else {
				dao.delete(FaultContinuedTimeCount.class, "year=? and month=?", year, month);
			}
		} else if (null == month) {
			// 删除当年 month为null的数据
			dao.delete(FaultContinuedTimeCount.class, "year=? and month is null", year);
		}
	}

	public void deleteAllFaultCount() {
		dao.deleteAll(FaultContinuedTimeCount.class);
	}

	/**
	 * ******************************保护动作时间*************************************
	 */
	/**
	 * 查询年度数据（month为null）
	 * 
	 * @param year
	 * @param voltage
	 * @param subCompany
	 * @return
	 */
	public List<ProtectActTimeCount> findYearProtectActTimeCount(Integer year, Integer voltage, String subCompany) {
		List<ProtectActTimeCount> list = new ArrayList<ProtectActTimeCount>();
		if (null != year) {
			List<Object> values = new ArrayList<>();
			String sql = "year = ? and month is null";
			values.add(year);
			if (StringUtils.isNotEmpty(subCompany)) {
				sql += " and subCompany = ? ";
				values.add(subCompany);
			}
			if (voltage != null) {
				sql += " and voltage = ? ";
				values.add(voltage);
			}
			sql += " order by year";
			list = dao.findByCondition(ProtectActTimeCount.class, sql, values.toArray());
		}
		return list;
	}

	/**
	 * 查询月度数据
	 * 
	 * @param year
	 * @param month      为null，则查询1-12月份的数据
	 * @param voltage
	 * @param subCompany
	 * @return
	 */
	public List<ProtectActTimeCount> findMonthProtectActTimeCount(Integer year, Integer month, Integer voltage, String subCompany) {
		List<ProtectActTimeCount> list = new ArrayList<ProtectActTimeCount>();
		if (null != year) {
			List<Object> values = new ArrayList<>();
			String sql = "year = ? ";
			values.add(year);
			if (month != null) {
				sql += " and month = ? ";
				values.add(month);
			} else {
				sql += " and month is not null ";
			}
			if (StringUtils.isNotEmpty(subCompany)) {
				sql += " and subCompany = ? ";
				values.add(subCompany);
			}
			if (voltage != null) {
				sql += " and voltage = ? ";
				values.add(voltage);
			}
			sql += " order by month";
			list = dao.findByCondition(ProtectActTimeCount.class, sql, values.toArray());
		}
		return list;
	}

	public void deleteProtectActTimeCount(Integer year, Integer month) {
		if (null != month) {
			if (0 == month) {
				// 删除当年1-12月份的数据
				dao.delete(ProtectActTimeCount.class, "year=? and month is not null", year);
			} else {
				dao.delete(ProtectActTimeCount.class, "year=? and month=?", year, month);
			}
		} else if (null == month) {
			// 删除当年 month为null的数据
			dao.delete(ProtectActTimeCount.class, "year=? and month is null", year);
		}
	}

	public void deleteProtectActTimeCount(String id) {
		dao.delete(id, ProtectActTimeCount.class);
	}

	public void deleteBreakerActTimeCount(String id) {
		dao.delete(id, BreakerActTimeCount.class);
	}

	public void deleteAllProtectActTime() {
		dao.deleteAll(ProtectActTimeCount.class);
	}

	public void createOrUpdateAct(ProtectActTimeCount bean) {
		dao.createOrUpdate(bean, ProtectActTimeCount.class);
	}

	public void createOrUpdateBreakerAct(BreakerActTimeCount bean) {
		dao.createOrUpdate(bean, BreakerActTimeCount.class);
	}

	/**
	 * ******************************断路器动作时间*************************************
	 */
	/**
	 * 查询年度数据（month为null）
	 * 
	 * @param year
	 * @param voltage
	 * @param subCompany
	 * @return
	 */
	public List<BreakerActTimeCount> findYearBreakerActTimeCount(Integer year, Integer voltage, String subCompany) {
		List<BreakerActTimeCount> list = new ArrayList<BreakerActTimeCount>();
		if (null != year) {
			List<Object> values = new ArrayList<>();
			String sql = "year = ? and month is null";
			values.add(year);
			if (StringUtils.isNotEmpty(subCompany)) {
				sql += " and subCompany = ? ";
				values.add(subCompany);
			}
			if (voltage != null) {
				sql += " and voltage = ? ";
				values.add(voltage);
			}
			sql += " order by year";
			list = dao.findByCondition(BreakerActTimeCount.class, sql, values.toArray());
		}
		return list;
	}

	/**
	 * 查询月度数据
	 * 
	 * @param year
	 * @param month      为null，则查询1-12月份的数据
	 * @param voltage
	 * @param subCompany
	 * @return
	 */
	public List<BreakerActTimeCount> findMonthBreakerActTimeCount(Integer year, Integer month, Integer voltage, String subCompany) {
		List<BreakerActTimeCount> list = new ArrayList<BreakerActTimeCount>();
		if (null != year) {
			List<Object> values = new ArrayList<>();
			String sql = "year = ? ";
			values.add(year);
			if (month != null) {
				sql += " and month = ? ";
				values.add(month);
			} else {
				sql += " and month is not null ";
			}
			if (StringUtils.isNotEmpty(subCompany)) {
				sql += " and subCompany = ? ";
				values.add(subCompany);
			}
			if (voltage != null) {
				sql += " and voltage = ? ";
				values.add(voltage);
			}
			sql += " order by year";
			list = dao.findByCondition(BreakerActTimeCount.class, sql, values.toArray());
		}
		return list;
	}

	public void deleteBreakerActTimeCount(Integer year, Integer month) {
		if (null != month) {
			if (0 == month) {
				// 删除当年1-12月份的数据
				dao.delete(BreakerActTimeCount.class, "year=? and month is not null", year);
			} else {
				dao.delete(BreakerActTimeCount.class, "year=? and month=?", year, month);
			}
		} else if (null == month) {
			// 删除当年 month为null的数据
			dao.delete(BreakerActTimeCount.class, "year=? and month is null", year);
		}
	}

	public void deleteAllbreakerActTime() {
		dao.deleteAll(BreakerActTimeCount.class);
	}

	public interface FaultContinuedTimeCountstrategy {
		List<?> findActTimeCount(Integer year, Integer month, Integer voltage, String subCompany);

		//年份，月份，电压等级，调管单位，开始范围，结束范围
		List<?> findActTimeCount(Integer year, Integer month, Integer voltage, String subCompany, Integer range1, Integer range2,String type);
	}

	/**
	 * 根据OMS导入数据统计策略
	 * 
	 * @author
	 */
	public class OMSCountstrategy implements FaultContinuedTimeCountstrategy {
		SuperDAO dao = SpringContext.getBean(SuperDAO.class);

		@Override
		public List<?> findActTimeCount(Integer year, Integer month, Integer voltage, String subCompany) {
			List<?> list = null;
			String sql = "select * from t_cim_fault_oms  where  1=1  ";
			List<Object> values = new ArrayList<>();
			if (null != year) {
				sql += " and faultDate between ?  and ?";
				Date start = DateUtil.getDateByString(year + "-01-01", DateUtil.pattern_ymd);
				Date end = DateUtil.getDateByString(year + "-12-31", DateUtil.pattern_ymd);
				values.add(start);
				values.add(end);
			}
			if (voltage != null) {
				if(voltage !=0) {
					sql += " and  voltage = ? ";
					values.add(Float.valueOf(voltage.toString()));
				}else {
					sql += " and  voltage >= ? ";
					values.add(Float.valueOf(220));
				}
			}
			sql += "order by voltage desc";
			list = dao.nativeQuery(sql, values.toArray());
			return list;
		}

		@Override
		public List<?> findActTimeCount(Integer year, Integer month, Integer voltage, String subCompany, Integer range1, Integer range2,String type) {
			List<?> list = null;
			String sql = "select * from t_cim_fault_oms  where  1=1  ";
			List<Object> values = new ArrayList<>();
			if (null != year) {
				sql += " and faultDate between ?  and ?";
				Date start = DateUtil.getDateByString(year + "-01-01", DateUtil.pattern_ymd);
				Date end = DateUtil.getDateByString(year + "-12-31", DateUtil.pattern_ymd);
				if (null != month) {
					start = DateUtil.getDateByString(year + "-" + month + "-01", DateUtil.pattern_ymd);
					end = DateUtil.getDateByString(year + "-" + month + "-31", DateUtil.pattern_ymd);
				}
				values.add(start);
				values.add(end);
			}
			if (voltage != null) {
				if(voltage == 0) {
					sql += " and  voltage >= ? ";
					values.add(Float.valueOf(220));
				}else {
					sql += " and  voltage = ? ";
					values.add(Float.valueOf(voltage.toString()));
				}
			}
			if (range1 != null && range2 != null) {
				sql += " and "+ type +" >= ?  and " +type+" <?  ";
				values.add(range1);
				values.add(range2);
			}
			sql += " order by "+ type +" DESC";
			list = dao.nativeQuery(sql, values.toArray());
			return list;
		}
	}
	
	/**
	 * 根据
	 * @param year
	 * @param month
	 * @param range1
	 * @param range2
	 * @return
	 */
	public List<BreakerActionInfo> findBreakerActionInfoByYearMonthAndIntervalTimeRange(String startYear,String endYear, 
			Integer range1, Integer range2){
		List<BreakerActionInfo> list = new ArrayList<BreakerActionInfo>();
		if(null != startYear && null !=  endYear ) {
			String sql = "";
			List<Object> values = new ArrayList<>();
			    sql += "faultTime between ? and ? ";
				values.add(startYear + "-01");
				values.add(endYear + "-12");
			if(null != range1 && null != range2) {
			    sql += "and actionTimeInterval between ? and ? ";
				values.add(range1*1000);
				values.add(range2*1000);
			}
			sql += " order by faultTime DESC";
			list = dao.findByCondition(BreakerActionInfo.class, sql, values.toArray());
		}
		return list;
	}
	
}
